/*************************************************************************
	> File Name: P2512.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-02-17 15:36:58
 ************************************************************************/
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_N 1000000
long long arr[MAX_N + 5];
long long s[MAX_N + 5], f[MAX_N + 5];
int n;

int main() {
    cin >> n;
    s[0] = 0;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
        s[i] = s[i - 1] + arr[i];
    }
    for (int i = 1; i <= n; i++) {
        f[i] = s[i] - i * (s[n] / n);
    }
    sort(f + 1, f + 1 + n);
    long long tf = f[(n + 1) / 2], cost = 0;
    for (int i = 1; i <= n; i++) {
        cost += abs(f[i] - tf);
    }
    cout << cost << endl;
    return 0;
}
